 var def = "";
 var currentphotoid = 0;
 var nextPhoto = '';
 var currentPhoto = null;
 
//$('.al img').load(function() {
//	if(this.width < 240){
//	    var newsrc = this.src.replace("_m","");
//	    this.src = newsrc;
//    }
//});
String.prototype.format = function() {
	var s = this,
	    i = arguments.length;
	
	while (i--) {
	    s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
	}
    return s;
};

var timer, timerb;
var totalview = 0;

function hidedesc(post){
	post.find('.setcontainer').animate({ height: 'hide'}, 300, function() {});
	post.find('.stat').animate({ height: 'hide'}, 300, function() {});
}

function showDesc(post){
	post.find('.setcontainer').animate({ height: 'show'}, 300, function() {});
	post.find('.stat').animate({ width: 'show'}, 300, function() {});	
}
$(document).ready(function(){
	$(".post").hover(
		function () {
			$(this).find('.bottom').show();
			var a = $(this);
			clearTimeout(timer);
			timer = window.setTimeout(function() {
				showDesc(a);
			}, 600);
			
		},
		function () {
			$(this).find('.bottom').hide();
			var a = $(this);
			clearTimeout(timer);
			if($('#commentdiv').is(':visible'))
				return;			
			window.setTimeout(function() {
				hidedesc(a);
			}, 600);
		}
	);
	
	$(".set").hover(
		function () {
			var b = $(this);
			clearTimeout(timerb);
			timerb = window.setTimeout(function() {
				//a.find('.setcontainer').show();
				b.find('.exif').animate({ height: 'show'}, 150, function() {});
			}, 150);
		},
		function () {
			var b = $(this);
			clearTimeout(timerb);
			window.setTimeout(function() {
				b.find('.exif').animate({ height: 'hide'}, 150, function() {});
			}, 150);
		}
	);
	
	$(".post").each(function(){
		if($(this).find('.commentbtn').length > 0){
			var currentid = $(this).find('.commentbtn')[0].id;
			$.getJSON('/json/photodetails/' + currentid + '/', function(photo) {
//				var str = "<h2>{0}</h2><p>{1}</p><p>by <a href='/photos/{2}' >{3}</a> on <a href='{4}' >{5} {6}</a></p><h2>Stats</h2>Views: {7} Comments: {8}".format(photo.title, photo.description, photo.nsid, photo.ownername, photo.url, photo.date, photo.time, photo.views, photo.comments);
//				if (photo.exif != null && photo.exif != ''){
//					str += "<h2><a href='{0}meta'>Exif</a></h2>{1}".format(photo.url, photo.exif);
//				}
				
				var details = "<h3 class='title'>{0}</h3>".format(photo.title);
				if (photo.description != null && photo.description != '')
					details += "<p class='description'>{0}</p>".format(photo.description);
				else
					details += "<br/>";
				details += "<div style='float:left;margin-bottom:5px;' ><a href='/photos/{0}'>{1}</a><br/><a href='{2}' >{3} {4}</a></div><div style='float: right; text-align: right;'>{5} views<br>{6} comments</div>".format(photo.nsid, photo.ownername, photo.url, photo.date, photo.time, photo.views, photo.comments);
				totalview += parseInt(photo.views);
				$('#totalview').html(totalview + ' views on this page');
				
				if (photo.exif != null && photo.exif != '')
					details += "<div class='exif' style='clear:both;display:none;'>{0}</div>".format(photo.exif);
				
				$('#' + photo.id).parent().parent().find('.set').html(details);
								
				//$('#' + photo.id).parent().find('.desc').html(str);
				$('#' + photo.id).parent().parent().find('.ap').attr('href', photo.localurl);
				//$('#' + photo.id).parent().parent().find('.al .ap img').attr('title', photo.title);
				if(photo.isfavorite == "1"){
					$('#' + photo.id).parent().find('.favebtn').addClass("buttonactive");
				}
			});
		}
	});
});	

function addcomment(evt, photoID){
	currentphotoid = photoID;
	currentPhoto = $(evt).parent().parent();
	$('#commentdiv').css('left', currentPhoto.position().left + currentPhoto.width());
	$('#commentdiv').css('top', currentPhoto.position().top);
	if(currentPhoto.position().left + currentPhoto.width() + $('#commentdiv').width() > $(document).width()){
        $('#commentdiv').css('top', currentPhoto.position().top);
        $('#commentdiv').css('left', currentPhoto.position().left - $('#commentdiv').width() - 16);        
	}
	$('#commentdiv').animate({ width: 'show'}, 300, function() {});
	showDesc(currentPhoto);
	$('#commentbox').val('');
	$('#commentbox').focus();
	nextPhoto = currentPhoto.next().find('.commentbtn')[0];
	$.scrollTo({ 
		top: currentPhoto.position().top - 10, 
		left: currentPhoto.position().left - 12
	}, 800 );
	$('#overlay').css('z-index', 2).show().height($(document).height());	
	currentPhoto.css('z-index', 3);
	return false;
}

function addfave(evt, photoID){	
	$.post("/fave/", { photoid: photoID },
		function(data){
			if(data == 'fave')
				$(evt).removeClass('button').addClass('buttonactive');
			else if(data == 'unfave')
				$(evt).removeClass('buttonactive').addClass('button');
		}
	);
	return false;
}

$(function(){
	$('#commentbox').focus(function(){			
		if($('#commentbox').val() == 'Add comments')
			$('#commentbox').val('');
	})
	$('#commentbutton').click(function(){
		$('#commentbox').attr('disabled', 'true');
		
		$.post(
			"/comment/", 
			{ comment: $('#commentbox').val(), photoid: currentphotoid },
			function(data){
				$('#commentbox').val(data).removeAttr('disabled');
				window.setTimeout(function() {
					$('#nextbutton').click();
				}, 1200);
				
			}
		);
	});

	$('#cancelbutton').click(function(){			
		$('#commentdiv').hide('fast');
		currentPhoto.css('z-index', 1);
		hidedesc(currentPhoto);
		$('#overlay').hide();				
	});
	$('#nextbutton').click(function(){
		currentPhoto.css('z-index', 1);
		hidedesc(currentPhoto);
		$(nextPhoto).click();						
	});
});